package com.help10000.rms.ui.utils

import com.alibaba.fastjson.JSONArray
import com.amir.common.utils.LogUtils.Companion.E
import java.lang.reflect.Type

/**
 * @author justkeepmoving
 */
object JsonUtils {
    const val TAG = "JsonUtils"

    /**
     * 将对象字段小写 序列化
     *
     * @param object
     * @return
     */
    fun getString(`object`: Any?): String? {
        return try {
            if (`object` == null) {
                null
            } else com.alibaba.fastjson.JSONObject.toJSONString(`object`)
        } catch (e: Exception) {
            E(TAG, "parse user error:" + e.message)
            null
        }
    }


    fun <T> getBean(json: String?, t: Type?): T? {
        try {
            return com.alibaba.fastjson.JSONObject.parseObject(json, t)
        } catch (e: Exception) {
            E(TAG, "getBean parse  error:" + e.message)
            e.printStackTrace()
        }
        return null
    }

    fun <T> getBean(json: String?, t: Class<T>?): T? {
        try {
            return com.alibaba.fastjson.JSONObject.parseObject(json, t)
        } catch (e: Exception) {
            E(TAG, "getBean parse  error:" + e.message)
            e.printStackTrace()
        }
        return null
    }

    fun <T> getList(jsonString: String?, tClass: Class<T>?): List<T>? {
        try {
            JSONArray.parseArray(
                jsonString,
                tClass
            )
        } catch (e: Exception) {
            E(TAG, "getLatestList parse  error:" + e.message)
            e.printStackTrace()
        }
        return null
    }
}